import requests
import execjs
import time
SESSION = requests.session()
ASTContext = execjs.compile(open("ast.js", "r", encoding="utf-8").read())
BrowserEnvironment = """ 
var window = {};
window.navigator = {};
window.navigator.userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/532.36 (KHTML, like Gecko) Chrome/83.0.1127.05 Safari/527.16";
function getCookie() {
    return window.cookie;
}
var document = {
    createElement: function(tag){
        var innerHTML;
        return {
            firstChild: {
                href: "https://www.mafengwo.cn/"
            }
        }
    }
};
window.document = document;
var location = {"href": "https://www.mafengwo.cn/"};
window.location = location;
 setTimeout = function(fn) { // 直接hook setTimeout方法，让它立即执行
     fn.call(this)
 }
"""
def converJSCode(jsScript):
    code = ASTContext.call("compile", jsScript)
    # print(code)
    return code
def resolveJsClearance(content):
    content = content.lstrip("<script>").lstrip("document.cookie=").split(";location.href=")[0]
    # print(content)
    return execjs.eval("(%s)"%content).split(";")[0]
def get_content(url):
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
    }
    # 第一次521请求
    r = SESSION.get(url, headers=headers)
    print(r.text)
    print(r.status_code)
    # header头上的set-cookie字段
    __jsluid_s = r.headers["set-cookie"].split(";")[0]+";"
    
    # 新增的部分
    __temp_jsl_clearance = resolveJsClearance(r.text)
    print(__temp_jsl_clearance)

    # 两个cookie字段
    cookieStr = "%s%s"%(__jsluid_s, __temp_jsl_clearance)
    headers.update({"cookie": cookieStr})
    print(headers)

    # 第二次521请求，与以前的相同
    r2 = SESSION.get(url, headers=headers, timeout=3)
    # print(r2.text)
    jsScript = r2.text.strip("<script>").strip("</script>")
    # open("pre.js", "w", encoding="UTF-8").write(jsScript)

    execjsCode = BrowserEnvironment+converJSCode(jsScript)
    # open("after.js", "w", encoding="UTF-8").write(execjsCode)
    start = time.time()
    __jsl_clearance = execjs.compile(execjsCode).call("getCookie").split(";")[0]
    end = time.time()
    print(end-start)
    cookieStr = "%s%s"%(__jsluid_s, __jsl_clearance)
    headers.update({"cookie": cookieStr})

    # 正常请求了
    start = time.time()
    r3 = requests.get(url, headers=headers, timeout=3)
    end = time.time()
    print("request consume", end-start)
    print(r3.text)
get_content("https://www.mafengwo.cn/i/18252205.html")